package org.fatmansoft.teach.service;

import org.fatmansoft.teach.models.*;
import org.fatmansoft.teach.payload.request.DataRequest;
import org.fatmansoft.teach.payload.response.DataResponse;
import org.fatmansoft.teach.repository.*;
import org.fatmansoft.teach.util.ComDataUtil;
import org.fatmansoft.teach.util.CommonMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service

public class StudentHonorService {
    @Autowired
    StudentHonorRepository studentHonorRepository;

    @Autowired
    StudentStudentHonorRepository studentStudentHonorRepository;

    @Autowired
    StudentRepository studentRepository;
    /**************************************辅助方法*********************************************/

    //用于主键自增
    public synchronized Integer getNewStudentHonorId() {
        Integer id = studentHonorRepository.getMaxId();
        if(id == null)
            id = 1;
        else
            id = id +1;
        return id;
    }
    public synchronized  Integer getNewStudentStudentHonorId() {
        Integer id = studentStudentHonorRepository.getMaxId();
        if(id == null)
            id = 1;
        else
            id = id + 1;
        return id;
    }
    //获取Student实体
    public Student getStudent(Integer studentId) {

        Student student = null;
        if(studentId != null) {
            Optional<Student> x1 = studentRepository.findById(studentId);
            if(x1.isPresent()) {
                student = x1.get();
            }
        }
        return student;
    }
    public Student getStudentByNum(String num) {
        Student student = null;
        if(num != null) {
            Optional<Student> x = studentRepository.findByPersonNum(num);
            if(x.isPresent()) {
                student = x.get();
            }
        }
        return student;
    }
    //查找StudentHonor实体
    public StudentHonor getStudentHonor(Integer studentHonorId) {

        StudentHonor studentHonor = null;
        if(studentHonorId != null) {
            Optional<StudentHonor> x2 = studentHonorRepository.findById(studentHonorId);
            if(x2.isPresent()) {
                studentHonor = x2.get();
            }
        }

        return studentHonor;
    }

    public StudentHonor getStudentHonorByTitle(String title) {
        StudentHonor studentHonor = null;
        if(title != null) {
            Optional<StudentHonor> x = studentHonorRepository.findByTitle(title);
            if(x.isPresent()) {
                studentHonor = x.get();
            }
        }
        return studentHonor;
    }


    public StudentHonor getStudentHonorByTitleDate(String title, String date) {
        StudentHonor studentHonor = null;
        if(title != null) {
            Optional<StudentHonor> x = studentHonorRepository.findByTitleAndDate(title,date);
            if(x.isPresent()) {
                studentHonor = x.get();
            }
        }
        return studentHonor;
    }
    //得到信息的键值对
    public Map getMapFromStudentHonor(StudentHonor honor) {
        Map m = new HashMap();
        if(honor == null) {
            return m;
        }


        m.put("studentHonorId",honor.getStudentHonorId());
        m.put("title",honor.getTitle());
        m.put("level",honor.getLevel());
        String level = honor.getLevel();
        m.put("levelName", ComDataUtil.getInstance().getDictionaryLabelByValue("JBM", level)); //级别类型的值转换成数据类型名
        m.put("reward",honor.getReward());
        m.put("date",honor.getDate());

        return m;
    }
    public Map getMapFromStudentStudentHonor(StudentStudentHonor studentStudentHonor){
        Student student = studentStudentHonor.getStudent();
        StudentHonor honor = studentStudentHonor.getStudentHonor();
        Map m = new HashMap<>();
        m.put("num",student.getPerson().getNum());
        m.put("dept",student.getPerson().getDept());
        m.put("major",student.getMajor());
        m.put("name",student.getPerson().getName());
        //m.put("studentHonorId",honor.getStudentHonorId());
        m.put("title",honor.getTitle());
        m.put("level",honor.getLevel());
        String level = honor.getLevel();
        m.put("levelName", ComDataUtil.getInstance().getDictionaryLabelByValue("JBM", level)); //级别类型的值转换成数据类型名
        m.put("reward",honor.getReward());
        m.put("date",honor.getDate());
        return m;
    }

    public List getHonorMapList(String titleLevel) {
        List dataList = new ArrayList();
        List<StudentHonor> studentHonorList = studentHonorRepository.findHonorListByTitleLevel(titleLevel);
        if(studentHonorList == null || studentHonorList.size() == 0) {
            return dataList;
        }

        for(int i = 0;i < studentHonorList.size();i++) {
            dataList.add(getMapFromStudentHonor(studentHonorList.get(i)));
        }

        return dataList;
    }
    /********************************************接口业务逻辑部分****************************************/

    //插入新实体  ----------增
    public DataResponse saveStudentHonor(DataRequest dataRequest) {

        //获取数据
        String title = dataRequest.getString("title");
        String reward = dataRequest.getString("reward");
        String level = dataRequest.getString("level");
        String date = dataRequest.getString("date");

        StudentHonor studentHonor = getStudentHonorByTitle(title);
        if(studentHonor == null) {
            studentHonor = new StudentHonor();
            studentHonor.setStudentHonorId(getNewStudentHonorId());
        }
        //构造新实体

        studentHonor.setTitle(title);
        studentHonor.setReward(reward);
        studentHonor.setLevel(level);
        studentHonor.setDate(date);
        studentHonor.setStudentHonorId(getNewStudentHonorId());

        studentHonorRepository.save(studentHonor);

        return CommonMethod.getReturnMessageOK();
    }

    //删除实体  -----------删
    @Transactional
    public DataResponse deleteStudentHonor(DataRequest dataRequest) {
        //获取数据
        String title = dataRequest.getString("title");
        String date = dataRequest.getString("date");

        Optional<StudentHonor> studentHonor = studentHonorRepository.findByTitleAndDate(title,date);
        if (studentHonor == null)
            return CommonMethod.getReturnMessageError("没有该学生荣誉！");
        //删除
        studentHonorRepository.deleteByTitleAndDate(title,date);

        return CommonMethod.getReturnMessageOK();
    }

    //更新实体  ------------改
    public DataResponse update(DataRequest dataRequest) {

        //获取数据
        Integer id = dataRequest.getInteger("studentHonorId");
        String title = dataRequest.getString("title");
        String reward = dataRequest.getString("reward");
        String level = dataRequest.getString("level");
        String date = dataRequest.getString("date");

        //构造
        StudentHonor studentHonor = new StudentHonor();
        studentHonor.setTitle(title);
        studentHonor.setReward(reward);
        studentHonor.setLevel(level);
        studentHonor.setDate(date);
        studentHonor.setStudentHonorId(id);
        //更新
        studentHonorRepository.save(studentHonor);

        return CommonMethod.getReturnMessageOK();
    }
    //查找单个实体 -----------查
    public DataResponse find(DataRequest dataRequest) {

        //获取数据
        Integer id = dataRequest.getInteger("studentHonorId");

        //查找
        Optional<StudentHonor> studentHonor = studentHonorRepository.findById(id);

        if(studentHonor.isEmpty()) {
            return CommonMethod.getReturnMessageError("此学生荣誉不存在！");
        }

        return CommonMethod.getReturnData(studentHonor);
    }

    public DataResponse getStudentHonorInfo(DataRequest dataRequest) {

        //获取数据
        String title = dataRequest.getString("studentHonorTitle");
        String date = dataRequest.getString("date");

        //查找
        StudentHonor studentHonor = getStudentHonorByTitleDate(title, date);

        if(studentHonor == null) {
            return CommonMethod.getReturnMessageError("该学生荣誉不存在！");
        }

        return CommonMethod.getReturnData(getMapFromStudentHonor(studentHonor));
    }

    public DataResponse findByTitleLevel(DataRequest dataRequest) {
        String titleLevel = dataRequest.getString("titleLevel");
        List datalist = getHonorMapList(titleLevel);
        return CommonMethod.getReturnData(datalist);
    }
    /**************************************中间表部分*********************************************/

    //插入中间表
    public DataResponse insertPlus(DataRequest dataRequest) {
        //获取数据
        String studentNum = dataRequest.getString("num");
        String title = dataRequest.getString("title");
        Student student = getStudentByNum(studentNum);
        StudentHonor studentHonor = getStudentHonorByTitle(title);

        if(student == null)
            return CommonMethod.getReturnMessageError("学生不存在！");
        if( studentHonor == null)
            return CommonMethod.getReturnMessageError("该学生荣誉不存在！");

        StudentStudentHonor x = studentStudentHonorRepository.findStudentStudentHonorByStudentAndStudentHonor(student,studentHonor);
        //对于学生荣誉而言 一个学生可以获得多次同一项荣誉
        /*if(x != null) {
            return CommonMethod.getReturnMessageError("该学生已有此荣誉！");
        }*/
        //构建新实体
        StudentStudentHonor studentStudentHonor = new StudentStudentHonor();
        studentStudentHonor.setStudentStudentHonorId(getNewStudentStudentHonorId());
        studentStudentHonor.setStudent(student);
        studentStudentHonor.setStudentHonor(studentHonor);

        //插入表中
        studentStudentHonorRepository.save(studentStudentHonor);

        return CommonMethod.getReturnMessageOK();
    }

    //删除中间表的一项
    @Transactional
    public DataResponse deleteStudentStudentHonor(DataRequest dataRequest) {
            String num = dataRequest.getString("num");
            String title = dataRequest.getString("title");
            Student student = getStudentByNum(num);
            StudentHonor studentHonor = getStudentHonorByTitle(title);
            studentStudentHonorRepository.deleteByStudentAndStudentHonor(student, studentHonor);
            return CommonMethod.getReturnMessageOK();
    }

    //查找一个学生所有StudentHonor
    public DataResponse getStudentHonorByStudent(DataRequest dataRequest) {

        //获取数据
        String numName=dataRequest.getString("numName");

        //查找Student实体
        Student student=getStudentByNum(numName);

        List dataList = new ArrayList<>();
        List<StudentStudentHonor> sList = studentStudentHonorRepository.findByStudentPersonNum(numName);
        for(int i = 0;i < sList.size();i++) {
            dataList.add(getMapFromStudentStudentHonor(sList.get(i)));
        }
        sList.clear();
        sList = studentStudentHonorRepository.findByStudentPersonName(numName);
        for(int i = 0;i < sList.size();i++) {
            dataList.add(getMapFromStudentStudentHonor(sList.get(i)));
        }
        return CommonMethod.getReturnData(dataList);
    }
    //借助称号或级别查找一个特定学号学生所有StudentHonor
    public DataResponse getStudentHonorByTitleLevel(DataRequest dataRequest) {

        //获取数据
        String titleLevel=dataRequest.getString("titleLevel");
        String num=dataRequest.getString("num");

        //查找Student实体
        Student student=getStudentByNum(num);

        //查找StudentHonor实体
        StudentHonor studentHonor=getStudentHonorByTitle(titleLevel);

        List dataList = new ArrayList<>();
        List<StudentStudentHonor> sList = studentStudentHonorRepository.findByStudentHonorTitle(titleLevel);
        for(int i = 0;i < sList.size();i++) {
            if(sList.get(i).getStudent()==student){
            dataList.add(getMapFromStudentStudentHonor(sList.get(i)));}
        }
        sList.clear();
        sList = studentStudentHonorRepository.findByStudentHonorLevel(titleLevel);
        for(int i = 0;i < sList.size();i++) {
            if(sList.get(i).getStudent()==student){
            dataList.add(getMapFromStudentStudentHonor(sList.get(i)));}
        }
        return CommonMethod.getReturnData(dataList);
    }

    public DataResponse getStudentStudentHonorList(DataRequest dataRequest) {
        List dataList = new ArrayList<>();
        List<StudentStudentHonor> sList = studentStudentHonorRepository.findAll();
        if(sList == null || sList.size() == 0)
            return CommonMethod.getReturnData(dataList);
        for(int i = 0;i < sList.size();i++) {
            dataList.add(getMapFromStudentStudentHonor(sList.get(i)));
        }
        return CommonMethod.getReturnData(dataList);
    }

    //更改一个学生学生荣誉
    public DataResponse getStudentStudentHonorInfo(DataRequest dataRequest) {

        //获取数据
        String title = dataRequest.getString("studentStudentHonorTitle");
        String num =dataRequest.getString("num");
        StudentHonor studentHonor = getStudentHonorByTitle(title);
        Student student =getStudentByNum(num);

        //查找
        StudentStudentHonor studentStudentHonor = studentStudentHonorRepository.findStudentStudentHonorByStudentAndStudentHonor(student,studentHonor);

        if(studentStudentHonor == null) {
            return CommonMethod.getReturnMessageError("该学生荣誉不存在！");
        }

        return CommonMethod.getReturnData(getMapFromStudentStudentHonor(studentStudentHonor));
    }
}
